#include "CAnalysisNormals.h"


#include <vtkPolyDataNormals.h>
#include <vtkPolyDataMapper.h>
#include <vtkArrowSource.h>
#include <vtkGlyph3D.h>
#include <vtkMaskPoints.h>
#include <QDebug>
#include <vtkPointData.h>
#include <vtkConeSource.h>
#include <vtkTransform.h>
#include <vtkTransformPolyDataFilter.h>

void CAnalysisNormals::setData(vtkSmartPointer<vtkPolyData> mapper)
{
    _data=mapper;
}

vtkSmartPointer<vtkMapper> CAnalysisNormals::getOutputData()
{
    qDebug()<<"begin normals analysis ...";
    vtkSmartPointer<vtkPolyDataNormals> normFilter=
            vtkPolyDataNormals::New();
    normFilter->SetInputData(_data);
    normFilter->ComputeCellNormalsOn();
    normFilter->Update();

    vtkPointData*data=normFilter->GetOutput()->GetPointData();
    if(data)
    {
        vtkDataArray*normals=data->GetNormals();
        if(normals)
        {
            qDebug()<<"for points in every cell!";
            qDebug()<<normals->GetNumberOfTuples();
        }
    }
    vtkSmartPointer<vtkConeSource> cone=
            vtkConeSource::New();
    cone->SetResolution(60);
    cone->SetHeight(60);
    cone->SetRadius(20);

    vtkSmartPointer<vtkTransform> transfor=
            vtkTransform::New();
    transfor->RotateY(180);
    vtkSmartPointer<vtkTransformPolyDataFilter> tf=
            vtkTransformPolyDataFilter::New();
    tf->SetInputConnection(cone->GetOutputPort());
    tf->SetTransform(transfor);

    vtkSmartPointer<vtkGlyph3D> glyph=
            vtkGlyph3D::New();

    glyph->SetInputConnection(normFilter->GetOutputPort());
    glyph->SetSourceConnection(tf->GetOutputPort());
    glyph->SetVectorModeToUseNormal();
    glyph->SetScaleModeToScaleByVector();
    glyph->SetScaleFactor(0.1);
    glyph->Update();

    vtkSmartPointer<vtkPolyDataMapper> glyphMapper=
            vtkPolyDataMapper::New();
    glyphMapper->SetInputData(glyph->GetOutput());
    qDebug()<<"end normals analysis ...";

    return glyphMapper;
}




